package com.kyriec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kyriec.Util.EResult;
import com.kyriec.dto.OrderDTO;
import com.kyriec.entity.Goods;
import com.kyriec.entity.Items;
import com.kyriec.entity.Orders;
import com.kyriec.mapper.GoodsMapper;
import com.kyriec.mapper.ItemsMapper;
import com.kyriec.mapper.OrdersMapper;
import com.kyriec.service.GoodsService;
import com.kyriec.service.OrdersService;
import com.kyriec.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单(Orders)表服务实现类
 *
 * @author KyrieC
 * @since 2023-11-28 22:31:19
 */
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public EResult addOrders(Orders orders) {
//        Integer OrderId =  ordersMapper.insert(orders);
        ordersMapper.insert(orders);
        return EResult.success();
    }

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public EResult getOrdersByUserId(Integer userId) {

//        List<OrderDTO> orderDTOList = new ArrayList<>();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId);
        List<Orders> ordersList = ordersMapper.selectList(queryWrapper);

//        LambdaQueryWrapper<Items> itemsLambdaQueryWrapper = new LambdaQueryWrapper<>();
//
//        for(Orders orders : ordersList){
//            itemsLambdaQueryWrapper.eq(Items::getOrderId,orders.getId());
//            itemsMapper.selectList(itemsLambdaQueryWrapper);
//        }
//        List<OrderVo> orderVoList = new ArrayList<>();



        List<OrderDTO> orderDTOList = new ArrayList<>();
        for(Orders orders : ordersList){
            orderDTOList.add(new OrderDTO(orders,goodsMapper.selectById(orders.getGoodId())));
        }

        return EResult.success(orderDTOList);
    }


}

